package View;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;

import model.Player;

import test_kevin.TestMovePanel;

import View.Components.PictureJPanel;

import Controller.ActionController;

public class MainWindow extends JFrame implements ActionListener{
	private static final long serialVersionUID = -363876463957952848L;
	
	private GraphicsDevice videoCard;
	private DisplayMode disMode;
	public ControllPlayer _unnamed_ControllPlayer_;
	public FileChooser _unnamed_FileChooser_;
	public PlayList _unnamed_PlayList_;
	public ActionController _unnamed_ActionController_;

	private boolean fullScreenEnabled;
	private ActionController myController;
	private FileDirChooserFrame fileDirChoFrame;
	private ControllPlayer controlPlayer;
	private PlayList playlist;
	private FileChooser fileCho;
	private Player player;
	
	public MainWindow(ActionController myController){
		videoCard = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		@SuppressWarnings("unused")
		DisplayMode disMode = new DisplayMode(1024, 768, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.myController = myController;
		this.setupGUI();
	}

	public void setupGUI(){
		Container c = this.getContentPane();
		
		c.setLayout(null);
		
		Dimension workSize = new Dimension(1024,768);
		int margin = 10;
		
//		try {
//			BufferedImage bi = ImageIO.read(new File("src/test_kevin/mp3-icon.png"));
//			PictureJPanel pjp = new PictureJPanel(bi);
//			pjp.setSize(new Dimension(96/2,96/2));
//			pjp.setBounds(1050,0,96/2,96/2);
//			c.add(pjp);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		
		//TODO: testmovepanel und move testen
//		TestMovePanel
		
		fileDirChoFrame = new FileDirChooserFrame();
		System.out.println((int)(workSize.width/0.35));
		fileDirChoFrame.setBounds(0+margin, 0+margin, (int)(workSize.width*0.35) - margin*2, (int)(workSize.height*0.7) - margin*2);
		c.add(fileDirChoFrame);
		fileDirChoFrame.mother = this;
		
		fileCho = new FileChooser();
		fileDirChoFrame.setFileCho(fileCho);
		fileCho.setBounds((int)(workSize.width*0.35)+margin, 0+margin, (int)(workSize.width*0.65) - margin*2, (int)(workSize.height*0.7) - margin*2);
		c.add(fileCho);
		
		controlPlayer = new ControllPlayer(this);
		controlPlayer.setMother(this);
		controlPlayer.setBounds(0+margin, (int)(workSize.height*0.7)+margin, (int)(workSize.width*0.35) - margin*2, (int)(workSize.height*0.3) - margin*2-20);
		c.add(controlPlayer);
		
		playlist = new PlayList();
		controlPlayer.setPlaylist(playlist);
		playlist.setBounds((int)(workSize.width*0.35)+margin, (int)(workSize.height*0.7)+margin, (int)(workSize.width*0.65) - margin*2, (int)(workSize.height*0.3) - margin*2-20);
		c.add(playlist);
	}
	
	public void startActualRealSize() {
		this.setSize(new Dimension(1024,768));
		this.setVisible(true);
	}
	
	public void startMaxedScreen() {
		this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
		fullScreenEnabled = false;
		this.setVisible(true);
	}
	
	public void startFullscreen(){
		fullScreenEnabled = true;
		
		setBackground(Color.pink);
		setForeground(Color.white);
		
		try{
			this.setFullScreen(this.disMode);
			while(fullScreenEnabled){
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}finally{
			this.restoreScreen();
		}
	}
	
	public void setFullScreen(DisplayMode displMode){
		this.setUndecorated(true);
		this.setResizable(false);
		videoCard.setFullScreenWindow(this);
		
		if(displMode != null && videoCard.isDisplayChangeSupported()){
			try{
				videoCard.setDisplayMode(displMode);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	public Window getFullScreenWindow(){
		return videoCard.getFullScreenWindow();
	}
	
	public void restoreScreen(){
		Window w = videoCard.getFullScreenWindow();
		if(w != null){
			w.dispose();
		}
		videoCard.setFullScreenWindow(null);
	}
	
	
	/**
	 * This method return the variable videoCard.
	 * @return the videoCard
	 */
	public GraphicsDevice getVideoCard() {
		return videoCard;
	}

	/**
	 * This methods sets the variable videoCard.
	 * @param videoCard the videoCard to set
	 */
	public void setVideoCard(GraphicsDevice videoCard) {
		this.videoCard = videoCard;
	}

	/**
	 * This method return the variable disMode.
	 * @return the disMode
	 */
	public DisplayMode getDisMode() {
		return disMode;
	}

	/**
	 * This methods sets the variable disMode.
	 * @param disMode the disMode to set
	 */
	public void setDisMode(DisplayMode disMode) {
		this.disMode = disMode;
	}

	/**
	 * This method return the variable fullScreenEnabled.
	 * @return the fullScreenEnabled
	 */
	public boolean isFullScreenEnabled() {
		return fullScreenEnabled;
	}

	/**
	 * This methods sets the variable fullScreenEnabled.
	 * @param fullScreenEnabled the fullScreenEnabled to set
	 */
	public void setFullScreenEnabled(boolean fullScreenEnabled) {
		this.fullScreenEnabled = fullScreenEnabled;
	}

	/**
	 * This method returns the variable myController.
	 * @return the myController
	 */
	public ActionController getMyController() {
		return myController;
	}

	/**
	 * This method sets the variable myController.
	 * @param myController the myController to set
	 */
	public void setMyController(ActionController myController) {
		this.myController = myController;
	}

    /**
     * This method return the variable _unnamed_ControllPlayer_.
     * @return the _unnamed_ControllPlayer_
     */
    public ControllPlayer get_unnamed_ControllPlayer_() {
        return _unnamed_ControllPlayer_;
    }

    /**
     * This methods sets the variable _unnamed_ControllPlayer_.
     * @param _unnamed_ControllPlayer_ the _unnamed_ControllPlayer_ to set
     */
    public void set_unnamed_ControllPlayer_(ControllPlayer _unnamed_ControllPlayer_) {
        this._unnamed_ControllPlayer_ = _unnamed_ControllPlayer_;
    }

    /**
     * This method return the variable _unnamed_FileChooser_.
     * @return the _unnamed_FileChooser_
     */
    public FileChooser get_unnamed_FileChooser_() {
        return _unnamed_FileChooser_;
    }

    /**
     * This methods sets the variable _unnamed_FileChooser_.
     * @param _unnamed_FileChooser_ the _unnamed_FileChooser_ to set
     */
    public void set_unnamed_FileChooser_(FileChooser _unnamed_FileChooser_) {
        this._unnamed_FileChooser_ = _unnamed_FileChooser_;
    }

    /**
     * This method return the variable _unnamed_PlayList_.
     * @return the _unnamed_PlayList_
     */
    public PlayList get_unnamed_PlayList_() {
        return _unnamed_PlayList_;
    }

    /**
     * This methods sets the variable _unnamed_PlayList_.
     * @param _unnamed_PlayList_ the _unnamed_PlayList_ to set
     */
    public void set_unnamed_PlayList_(PlayList _unnamed_PlayList_) {
        this._unnamed_PlayList_ = _unnamed_PlayList_;
    }

    /**
     * This method return the variable _unnamed_ActionController_.
     * @return the _unnamed_ActionController_
     */
    public ActionController get_unnamed_ActionController_() {
        return _unnamed_ActionController_;
    }

    /**
     * This methods sets the variable _unnamed_ActionController_.
     * @param _unnamed_ActionController_ the _unnamed_ActionController_ to set
     */
    public void set_unnamed_ActionController_(
            ActionController _unnamed_ActionController_) {
        this._unnamed_ActionController_ = _unnamed_ActionController_;
    }

    /**
     * This method return the variable fileDirChoFrame.
     * @return the fileDirChoFrame
     */
    public FileDirChooserFrame getFileDirChoFrame() {
        return fileDirChoFrame;
    }

    /**
     * This methods sets the variable fileDirChoFrame.
     * @param fileDirChoFrame the fileDirChoFrame to set
     */
    public void setFileDirChoFrame(FileDirChooserFrame fileDirChoFrame) {
        this.fileDirChoFrame = fileDirChoFrame;
    }

    /**
     * This method return the variable controlPlayer.
     * @return the controlPlayer
     */
    public ControllPlayer getControlPlayer() {
        return controlPlayer;
    }

    /**
     * This methods sets the variable controlPlayer.
     * @param controlPlayer the controlPlayer to set
     */
    public void setControlPlayer(ControllPlayer controlPlayer) {
        this.controlPlayer = controlPlayer;
    }

    /**
     * This method return the variable playlist.
     * @return the playlist
     */
    public PlayList getPlaylist() {
        return playlist;
    }

    /**
     * This methods sets the variable playlist.
     * @param playlist the playlist to set
     */
    public void setPlaylist(PlayList playlist) {
        this.playlist = playlist;
    }

    /**
     * This method return the variable fileCho.
     * @return the fileCho
     */
    public FileChooser getFileCho() {
        return fileCho;
    }

    /**
     * This methods sets the variable fileCho.
     * @param fileCho the fileCho to set
     */
    public void setFileCho(FileChooser fileCho) {
        this.fileCho = fileCho;
    }

    /**
     * This method return the variable serialversionuid.
     * @return the serialversionuid
     */
    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    /**
     * This method return the variable player.
     * @return the player
     */
    public Player getPlayer() {
        return player;
    }

    /**
     * This methods sets the variable player.
     * @param player the player to set
     */
    public void setPlayer(Player player) {
        this.player = player;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if(ae.getSource() instanceof JButton){
            ;
        }
    }
}